let base_url = 'http://everydaytech.cn:1051';
let wheelData = {
	rewardNames: [], //转盘奖品名称数组
	colors: [
		"#FFF4D7",
		"#FFFFFF",
		"#FFF4D7",
		"#FFFFFF",
		"#F0F4D8",
		"#FFFFFF",
	], //转盘奖品区块对应背景颜色
	imageSrcArr: [
		"./img/daijinquan.png",
		"./img/jiangpin.png",
		"./img/hongbao.png",
		"./img/puwu.png",
		"./img/zhekou.png",
		"./img/youpin.png",
	],
	images: [],
	outsideRadius: 140, //转盘外圆的半径
	textRadius: 115, //转盘奖品位置距离圆心的距离
	insideRadius: 38, //转盘内圆的半径
	startAngle: 0, //开始角度

	isRun: false
}
let baseAngle;
let jiang;
function draw() {
	// 计算每块占的角度，弧度制
	baseAngle = Math.PI * 2 / 6;
	//生成图片对象
	for(let i = 0; i < wheelData.imageSrcArr.length; i++) {
		let img = new Image();
		img.src = wheelData.imageSrcArr[i];
		console.log(img);
		wheelData.images.push(img)
	}
	console.log(wheelData.images)
}

//画图
$(function() {
	var drawprizeNum = sessionStorage.getItem("drawprizeNum");
	if(drawprizeNum){
		changeNum(drawprizeNum);
	}
	draw() ;
	getPrizeInfoList();
	//生成图片对象
	$(".pointer").on("click", function() {
		if(wheelData.isRun) return false;
		wheelData.isRun = !wheelData.isRun;
		//	设值中奖的中奖号
//		var item = 1;
		var drawprizeNum = sessionStorage.getItem("drawprizeNum");
		if(drawprizeNum ==0){
			window.location.href="../wangbingying_WebUI//timeout.html"
			return
		}
		else
		{
			run();
		}
	
//请求中奖号码
	})
})
function changeNum(num_){
	document.getElementById('num').innerText = num_
}
function run(){
	var openid = sessionStorage.getItem('openid');
	$.ajax({
		url: "http://www.everydaytech.cn:1051/api/Prize/DrawPrizeInfo?openid="+openid,
		type: 'get',
		dataType: 'json',
		timeout: 1000,
		success: function(data, status) {
			console.log(data);
			jiang = data.data;
			if(data.success) {
				rotateFunc(data.data.prizeID-1)//开始转
				var drawprizeNum = sessionStorage.getItem("drawprizeNum");
            		sessionStorage.setItem("drawprizeNum", drawprizeNum - 1);
            		changeNum(drawprizeNum-1)
			}
		},
		fail: function(err, status) {
			console.log(err)
		}
	})
}
function rotateFunc(item) {
	console.log(baseAngle / Math.PI * 180)
	// 旋转角度 == 270°（当前第一个角度和指针位置的偏移量） - 奖品的位置 * 每块所占的角度 - 每块所占的角度的一半(指针指向区域的中间)
	angles = 360 * 3 / 4 - (item * baseAngle) - baseAngle / 2; // 因为第一个奖品是从0°开始的，即水平向右方向
	$('#wheelCanvas').rotate({
		angle: 0,
		animateTo: 270 - baseAngle / Math.PI * 180 * (item + 0.5) + 360 * 8, // 这里多旋转了5圈，圈数越多，转的越快
		duration: 5000,
		callback: function() { // 回调方法
			//			$("#rewardTip").html(wheelData.rewardNames[item])
			document.getElementById("jiangpin").innerText = jiang.prizeName;
			document.getElementById("jiangpinImg").src = wheelData.imageSrcArr[item]
			document.getElementById("award").style.display = 'flex';
			
			wheelData.isRun = !wheelData.isRun;

		}
	});

}

//画图形以及里面的字
function drawImg() {
	console.warn(wheelData.images)
	let canvas = document.getElementById("wheelCanvas");
	let ctx = canvas.getContext("2d");

	//	如果奖品名字太长,分行的行距
	let line_height = 25;
	let canvasW = canvas.width; // 画板的高度
	let canvasH = canvas.height; // 画板的宽度
	//在给定矩形内清空一个矩形
	ctx.clearRect(0, 0, canvasW, canvasH);
	//strokeStyle 选定画笔颜色
	ctx.strokeStyle = "#FFBE04"; // 红色
	//font 画布上文本内容的当前字体属性
	ctx.font = '14px Microsoft YaHei';
	for(let i = 0; i < wheelData.rewardNames.length; i++) {

		let img = new Image();

		// 当前的角度
		let angle = wheelData.startAngle + i * baseAngle;
		// 填充颜色
		ctx.fillStyle = wheelData.colors[i];
		ctx.beginPath();
		/*
         * 画圆弧，和IOS的Quartz2D类似
//       * context.arc(x,y,r,sAngle,eAngle,counterclockwise);
         * x :圆的中心点x
         * y :圆的中心点x
         * sAngle,eAngle :起始角度、结束角度
         * counterclockwise : 绘制方向,可选，False = 顺时针，true = 逆时针
         * */

		ctx.arc(canvasW * 0.5, canvasH * 0.5, wheelData.outsideRadius, angle, angle + baseAngle, false);
		ctx.arc(canvasW * 0.5, canvasH * 0.5, wheelData.insideRadius, angle + baseAngle, angle, true);
		ctx.stroke();
		ctx.fill();
		//保存画布的状态，和图形上下文栈类似，后面可以Restore还原状态（坐标还原为当前的0，0），
		ctx.save();

		//      重新选取颜色
		ctx.fillStyle = "#000000";
		let rewardName = wheelData.rewardNames[i];

		let translateX = canvasW * 0.5 + Math.cos(angle + baseAngle / 2) * wheelData.textRadius;
		let translateY = canvasH * 0.5 + Math.sin(angle + baseAngle / 2) * wheelData.textRadius;
		ctx.translate(translateX, translateY);

		//      将画布旋转一定角度然后开始填充字
		ctx.rotate(angle + baseAngle / 2 + Math.PI / 2);
		//		根据奖品名称决定要不要分行
		if(wheelData.rewardNames[i].length > 8) {
			let newName = wheelData.rewardNames[i].substring(0, 8) + "," + wheelData.rewardNames[i].substring(8);
			let nameArr = newName.split(',');
			for(let j = 0; j < nameArr.length; j++) {
				ctx.fillText(nameArr[j], -ctx.measureText(nameArr[j]).width / 2, j * line_height)
			}

		} else {
			ctx.fillText(rewardName, -ctx.measureText(wheelData.rewardNames[i]).width / 2, 0)
		}
		ctx.drawImage(wheelData.images[i], -20, 5);
		//		img.src = wheelData.imageSrcArr[i];
		//      }
		ctx.restore();
	}
}

function getPrizeInfoList() {
	$.ajax({
		url: "http://www.everydaytech.cn:1051/api/Prize/GetPrizeInfoList",
		type: 'get',
		dataType: 'json',
		timeout: 1000,
		success: function(data, status) {
			console.log(data)
			if(data.success) {
				var list = data.data;
				for(var i = 0; i < list.length; i++) {
					wheelData.rewardNames.push(list[i].prizeName);
				}
				console.log(wheelData.rewardNames)
				console.log(wheelData.imageSrcArr)
				setTimeout(drawImg,500)
			}
		},
		fail: function(err, status) {
			console.log(err)
		}
	})

}